package krow.dev.gunpobus.view.fragment;

import java.util.ArrayList;
import java.util.List;

import krow.dev.gunpobus.Constants.Invoke;
import krow.dev.gunpobus.GlobalStore;
import krow.dev.gunpobus.R;
import krow.dev.gunpobus.ScheduleActivity;
import krow.dev.gunpobus.model.StationItem;
import krow.dev.gunpobus.util.CastUtil;
import krow.dev.gunpobus.util.InitialSearcher;
import krow.dev.gunpobus.view.SearchViewWrapper;
import krow.dev.gunpobus.view.adapter.StationListAdapter;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

public class StationFragment extends BaseFragment {

	private Context mContext;

	private SearchViewWrapper mSearchViewWrapper;
	private StationListAdapter mStationListAdapter;
	private ListView mStationListView;

	private List<StationItem> mStationItemList;
//	private Handler mHandler = new Handler();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		mContext = inflater.getContext();

		View view = inflater.inflate(R.layout.fragment_station, null);

		mSearchViewWrapper = new SearchViewWrapper(mContext);
		mSearchViewWrapper.setHint(getString(R.string.hint_search_station));
		mSearchViewWrapper.addTextChangedListener(mKeywordTextWatcher);
		mStationListAdapter = new StationListAdapter(mContext);
		mStationListView = (ListView) view.findViewById(R.id.listview_station);
		mStationListView.addHeaderView(mSearchViewWrapper.getView());
		mStationListView.addFooterView(inflater.inflate(R.layout.footer_dummy, null));
		mStationListView.setAdapter(mStationListAdapter);
		mStationListView.setOnItemClickListener(mItemClickListener);

		return view;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		if(GlobalStore.getInstance().isAvailable()){
			requestStation();
		}
	}

	@Override
	public void onResume() {
		super.onResume();

	}

	@Override
	public void onPause() {

		super.onPause();
	}
	
	@Override
	public void onDestroy() {
		
//		mHandler.removeCallbacks(mRequestFailureRunnable);
		
		super.onDestroy();
	}

//	@Override
//	protected void onRetry() {
//		super.onRetry();
//
//		requestStation();
//	}
	
	private void requestStation() {

		mStationItemList = GlobalStore.getInstance().getStationItemList();
		mStationListAdapter.setStationItemList(mStationItemList);
		mStationListAdapter.notifyDataSetChanged();
		
//		setLoadBarVisibility(View.VISIBLE);
//		RequestManager.requestStation(mResponseListener);
	}

	/*
	private HtmlResponseListener<StationParser> mResponseListener = new HtmlResponseListener<StationParser>() {

		@Override
		public void onResponse(StationParser response) {
			
			setLoadBarVisibility(View.GONE);
			
			mStationItemList = response.getRouteItemList();
			mStationListAdapter.setStationItemList(mStationItemList);
			mStationListAdapter.notifyDataSetChanged();
		}

		@Override
		public void onErrorResponse(VolleyError error) {

			mHandler.postDelayed(mRequestFailureRunnable, 1000);
		}
	};
	
	private Runnable mRequestFailureRunnable = new Runnable() {
		
		@Override
		public void run() {
			
			setLoadBarVisibility(View.GONE);
			mStationListAdapter.clear();
			showRetryAlert();
		}
	};
	*/
	
	private TextWatcher mKeywordTextWatcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {

		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {

		}

		@Override
		public void afterTextChanged(Editable s) {
			if (mStationItemList == null) {
				return;
			}

			String keyword = s.toString();
			if (keyword.length() > 0) {
				search(keyword);
				
			}else{
				mStationListAdapter.setStationItemList(mStationItemList);
			}
			mStationListAdapter.notifyDataSetChanged();
		}
		
		private void search(String keyword){
			
			List<StationItem> selectedItemList = new ArrayList<StationItem>();
			
			for (StationItem stationItem : mStationItemList) {
				try {
					if(CastUtil.isInteger(keyword)){
						if(InitialSearcher.match(stationItem.getSerialNumber(), keyword)){
							selectedItemList.add(stationItem);
						}
					}else{
						if(InitialSearcher.match(stationItem.getName(), keyword)){
							selectedItemList.add(stationItem);
						}
					}
				} catch (Exception e) {
					
				}
			}
			mStationListAdapter.setStationItemList(selectedItemList);
		}
	};
	
	private AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
			if(view.getTag() == null){
				return ;
			}
			
			StationItem stationItem = (StationItem) view.getTag();
			
			Intent intent = new Intent(getActivity(), ScheduleActivity.class);
			intent.putExtra(Invoke.KEY_STATION_NAME, stationItem.getName());
			intent.putExtra(Invoke.KEY_STATION_ID, stationItem.getId());
			intent.putExtra(Invoke.KEY_STATION_SERIAL, stationItem.getSerialNumber());
			startActivity(intent);
			getActivity().overridePendingTransition(R.anim.right_in, R.anim.scale_out);
		}
	};
}
